<template>
	<!-- 我的申请 -->
	<view class="my-apply">
		<picker :range="pickerRange" @change="bindPickerChange" :disabled="pickerDisabled">
			<view class="tab-posi">
				<NewSegmentedControl :current="current" :values="items" style-type="text" active-color="#198a6c"
					@clickItem="onClickItem" />
			</view>
		</picker>
		<view class="content">
			<view class="card" v-for="item in myApplyList" :key="item.taskId" @click="toDetail(item)">
				<view class="head">
					<text>{{item.taskTypeCh}}</text>
					<text v-if="item.taskSubTypeCh==null" style="background-color: #fff;"></text>
					<text v-if="item.taskSubTypeCh!==null" :class="{'color-pu-bg':item.taskSubTypeCh==='运维','color-r-bg':item.taskSubTypeCh==='报废'}">{{item.taskSubTypeCh}}</text>
				</view>
				<view class="list">
					<view> 处理状态：<text>{{item.taskStatusCh||'--'}}</text> </view>
					<view>申请编号：{{item.taskNo}}</view>
					<view>申请人：{{item.taskCreateUserName}}</view>
					<view>申请单位：{{item.organName||'--'}}</view>
					<view>申请时间：{{item.taskCreateTime||'--'}}</view>
					<view class="remark">备注信息：{{item.taskRemark||'--'}}</view>
					<text class="iconfont icon-jinrujiantouxiao"></text>
				</view>
			</view>
		</view>
		<!-- 加载结束 -->
		<view class="uni-loadmore" v-if="showLoadMore">{{loadMoreText}}</view>
	</view>
</template>

<script setup lang="ts">
	import {
		ref
	} from "vue";
	import {
		onPullDownRefresh,
		onReachBottom,
		onShow
	} from "@dcloudio/uni-app";
	import {
		applyList,
		addrList,
		getAllEnum,
		applyPerson,
		getAssetId
	} from "@/common/api/myApply";
	import {
		CONSTANT
	} from "@/common/config/config.ts";
	import {
		fromSearch
	} from "@/common/util/tabSearchUse.ts";

	const current = ref(0);
	const items = ref(["申请单位", "申请人", "申请类型"]);
	const pickerRange = ref();
	const pickerDisabled = ref(false);
	const currentTab = ref(0)
	const myApplyList = ref([]);
	const address = ref([])
	const applyType = ref([])
	const person = ref([])
	const page = ref(CONSTANT.initPage);
	const rows = ref(CONSTANT.pageSize);
	const allPages = ref(0);
	const showLoadMore = ref(false);
	const loadMoreText = ref("加载中...");
	const searchParams = ref({
		organId: '',
		taskCreateUser: '',
		taskType: '',

	});
	const matchRule = {
		organId: 'eq',
		taskCreateUser: 'eq',
		taskType: 'eq',
	};
	interface EnumType {
		label: string,
			value: string
	}

	const getApplyList = () => {
		const params = Object.assign(fromSearch(searchParams.value, matchRule), {
			page: page.value,
			rows: rows.value,
		});
		loadMoreText.value = '加载中...'
		applyList(params).then((res: any) => {
			uni.stopPullDownRefresh();
			if (res.code == 0) {
				allPages.value = res.data.pages;
				page.value = res.data.current;
				myApplyList.value = res.data.records;
				// console.log(myApplyList.value);
				myApplyList.value.forEach((item: any) => {
					item.taskCreateTime = item.taskCreateTime?.replace('T', ' ')
				})
				if (myApplyList.value.length === 0) {
					showLoadMore.value = true;
					loadMoreText.value = "暂无数据";
				} else if (myApplyList.value.length < CONSTANT.pageSize) {
					showLoadMore.value = true;
					loadMoreText.value = "没有更多的数据了";
				} else {
					showLoadMore.value = false;
				}
			} else {
				uni.showToast({
					icon: "none",
					title: res.message,
				});
			}
		});
	};
	getApplyList();

	const getApplyAddr = () => {
		addrList().then((res: any) => {
			if (res.code == 0) {				
				address.value = res.data.records.map((item: any) => {
					return {
						label: item.storeName,
						value: item.id
					}
				})
			}
		})
		getAllEnum('TaskType').then((res: any) => {
			if (res.code == 0) {
				applyType.value = res.data['TaskType'].map((item: any) => {
					return {
						label: item.value,
						value: item.code
					}
				})
			}
		})
		applyPerson().then((res: any) => {
			if (res.code == 0) {
				person.value = res.data.records.map((item: any) => {
					return {
						label: item.nickName,
						value: item.id
					}
				})
			}
		})
	}
	getApplyAddr()

	const onClickItem = (e: {
		currentIndex: number
	}) => {
		page.value = CONSTANT.initPage
		currentTab.value = e.currentIndex
		switch (e.currentIndex) {
			case 0:
				pickerRange.value = address.value.map((item: EnumType) => item.label);
				pickerRange.value.push('全部')
				items.value[0] = pickerRange.value[e.detail.value]
				searchParams.value.organId = (address.value[e.detail?.value] as EnumType).value
				break
			case 1:
				pickerRange.value = person.value.map((item: EnumType) => item.label);
				pickerRange.value.push('全部')
				break
			case 2:
				pickerRange.value = applyType.value.map((item:EnumType) => item.label);
				pickerRange.value.push('全部')
				break
		}

	};
	// picker选择切换时触发
	const bindPickerChange = (e: any) => {
		// console.log('tab切换',e.detail.value);
		let len = pickerRange.value.length-1
		myApplyList.value = []
		if (currentTab.value == 0) {
			if(e.detail.value==len){
				items.value[0] = '申请单位'
				searchParams.value.organId=''
			}else{
				items.value[0] = pickerRange.value[e.detail.value]
				searchParams.value.organId = (address.value[e.detail.value] as EnumType).value
			}
		} else if (currentTab.value == 1) {
			if(e.detail.value==len){
				items.value[1] = '申请人'
				searchParams.value.taskCreateUser = ''
			}else{
				items.value[1] = pickerRange.value[e.detail.value]
				searchParams.value.taskCreateUser = (person.value[e.detail.value] as EnumType).value
			}
		} else if (currentTab.value == 2) {
			if(e.detail.value==len){
				items.value[2] = '申请类型'
				searchParams.value.taskType = ''
			}else{
				items.value[2] = pickerRange.value[e.detail.value]
				searchParams.value.taskType = (applyType.value[e.detail.value] as EnumType).value
			}
			
		}
		getApplyList()
	};
	const toDetail = (item: any) => {
		switch (item.sysModuleCh) {
			case '资产申请':
				getAssetId(item.taskId).then((res:any)=>{
					if(res.code==0){
						switch (item.taskSubType/1) {
							case 1:
								uni.navigateTo({
									url: '/pages/assets/assetReceive/applyDetList?assetApplyId=' + item.taskId,//领用
								});
								break;
							case 2:
							case 3:
							case 4:
								uni.navigateTo({
									url: `/pages/assets/assetManage/index?assetId=${res.data}`,//报修、报废、退还
								});
								break;
						}
					}
				})
				
				break;
			case '巡检管理':
				uni.navigateTo({
					url: `/pages/ipqc/inspectDetail/index?rowData=${JSON.stringify(item.taskParam)}`
				});
				break;
			case '资产盘点':
				uni.navigateTo({
					url: `/pages/checkAssets/checkDetail/index?checkStoreId=${item.taskParam.checkStoreId}&version=${item.taskParam.version}`
				});
				break;
		}


	}
	onPullDownRefresh(() => {
		page.value = CONSTANT.initPage;
		myApplyList.value = [];
		getApplyList();
	});
	onReachBottom(() => {
		if (page.value > allPages.value || page.value == allPages.value) {
			loadMoreText.value = '没有更多的数据了'
			showLoadMore.value = true;
		} else {
			page.value++;
			loadMoreText.value = '加载中……';
			showLoadMore.value = true;
			getApplyList();
		}

	})
	// onShow(()=>{
	// 	getApplyAddr()
	// })
</script>

<style scoped lang="scss">
page{
	background-color: #eee !important;
}
</style>
